package tcp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;

public class TesteClientSocket2 extends Thread {

    private Socket sock;
    private BufferedReader in;
    private String host;
    private int port;

    public TesteClientSocket2(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void run() {
        boolean eof = true;

        while (eof) {
            try {
                sock = new Socket(host, port);
                in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                //String data = in.readLine();

                while (true) {
                    String data = in.readLine();
                    if (data != null) {
                        System.out.println(getName() + " " + data);
                    } else {
                        eof = false;
                    }
                }
            } catch (Exception e) {
                System.out.println("Erro de IO: " + e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        TesteClientSocket2 client = new TesteClientSocket2("127.0.0.1", 4413);
        client.setName("Thread 1");
        client.start();
        TesteClientSocket2 client2 = new TesteClientSocket2("127.0.0.1", 4413);
        client2.setName("Thread 2");
        client2.start();

    }
}
